package com.lin.missyou.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.lin.missyou.util.GenericAndJson;
import com.lin.missyou.util.ListAndJson;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Java类中可以写业务逻辑
 * @author gcq
 * @Create 2021-10-14
 */
@Entity
@Setter
@Getter
@Where(clause = "delete_time is null and online = 1")
public class Sku extends BaseEntity{

    @Id
    private Long id;
    /**
     * 价格
     */
    private BigDecimal price;
    /**
     * 折扣价
     */
    private BigDecimal discountPrice;
    private Boolean online;
    private String img;
    private String title;
    /**
     * spuid
     */
    private Long spuId;
    /**
     * 分类Id
     */
    private Long categoryId;
    /**
     * 根节点分类Id
     */
    private Long rootCategoryId;
    /**
     * 规格
     */
//    @Convert(converter = ListAndJson.class)
//    private List<Object> specs;
    private String specs;


    public BigDecimal getActualPrice()
    {
        return discountPrice == null ? this.price : this.discountPrice;
    }

    public List<Spec> getSpecs() {
        if(this.specs == null){
            return null;
        }
        return GenericAndJson.jsonToList(this.specs);
    }

    public void setSpecs(String specs) {
        if(specs.isEmpty()){
            return;
        }
        this.specs = GenericAndJson.objectToJson(specs);
    }

    /**
     * 获取所有spec值
     * 不需要返回到前端 加上注解 JsonIgnore
     * @return
     */
    @JsonIgnore
    public List<String> getSpecValueList()
    {
        return this.getSpecs().stream().map(Spec::getValue).collect(Collectors.toList());
    }

    /**
     * 唯一性 可以解析
     */
    private String code;
    /**
     * 库存量
     */
    private Long stock;
}